package org.dromara.tearoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.tearoom.domain.TerIotDevice;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerHornLoginBo;
import org.dromara.tearoom.domain.vo.TerHornLoginVo;
import org.dromara.tearoom.domain.TerHornLogin;
import org.dromara.tearoom.mapper.TerHornLoginMapper;
import org.dromara.tearoom.service.ITerHornLoginService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 喇叭登陆cookie信息Service业务层处理
 *
 * @author xch
 * @date 2025-04-23
 */
@RequiredArgsConstructor
@Service
public class TerHornLoginServiceImpl implements ITerHornLoginService {

    private final TerHornLoginMapper baseMapper;

    /**
     * 查询喇叭登陆cookie信息
     *
     * @param id 主键
     * @return 喇叭登陆cookie信息
     */
    @Override
    public TerHornLoginVo queryById(Long id){
        TerHornLoginBo bo = new TerHornLoginBo();
        bo.setDeviceId(id);
        LambdaQueryWrapper<TerHornLogin> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 分页查询喇叭登陆cookie信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 喇叭登陆cookie信息分页列表
     */
    @Override
    public TableDataInfo<TerHornLoginVo> queryPageList(TerHornLoginBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerHornLogin> lqw = buildQueryWrapper(bo);
        Page<TerHornLoginVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的喇叭登陆cookie信息列表
     *
     * @param bo 查询条件
     * @return 喇叭登陆cookie信息列表
     */
    @Override
    public List<TerHornLoginVo> queryList(TerHornLoginBo bo) {
        LambdaQueryWrapper<TerHornLogin> lqw =  bo!=null?buildQueryWrapper(bo): Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerHornLogin> buildQueryWrapper(TerHornLoginBo bo) {
        LambdaQueryWrapper<TerHornLogin> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TerHornLogin::getId);
        lqw.eq(bo.getDeviceId() != null, TerHornLogin::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getCookieValue()), TerHornLogin::getCookieValue, bo.getCookieValue());
        lqw.eq(bo.getModifyTime() != null, TerHornLogin::getModifyTime, bo.getModifyTime());
        return lqw;
    }

    /**
     * 新增喇叭登陆cookie信息
     *
     * @param bo 喇叭登陆cookie信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerHornLoginBo bo) {
        TerHornLogin add = MapstructUtils.convert(bo, TerHornLogin.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改喇叭登陆cookie信息
     *
     * @param bo 喇叭登陆cookie信息
     * @return 是否修改成功
     */
    @Override
    public int updateByBo(TerHornLoginBo bo) {
        return baseMapper.update(null,
                new LambdaUpdateWrapper<TerHornLogin>()
                        .eq(TerHornLogin::getDeviceId, bo.getDeviceId())
                        .set(TerHornLogin::getCookieValue, bo.getCookieValue())
                        .set(TerHornLogin::getModifyTime, bo.getModifyTime()));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerHornLogin entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除喇叭登陆cookie信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public TerHornLoginVo queryByDeviceId(TerHornLoginBo bo) {
        LambdaQueryWrapper<TerHornLogin> lqw = Wrappers.lambdaQuery();
        lqw.eq(TerHornLogin::getDeviceId, bo.getDeviceId());
        return  baseMapper.selectVoOne(lqw);
    }
}
